<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\RouteAction;
use App\Http\Controllers\Controller;
use App\Models\Goods;
use App\Services\GoodService;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class GoodCollectionController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('收藏商品')
            ->description('已收藏商品列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Goods);
        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->actions(function ($actions) {

            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            $actions->add(new RouteAction(3));
        });
        $grid->model()->where('star', 1)->orderBy('updated_at', 'desc');

        $grid->item_id('商品ID');
        $grid->isbn('ISBN');
        $grid->title('商品名')->editable();
        $grid->img_url('图片')->image(100, 100)->modal(function ($modal) {
            $big_img = str_replace("_s", "", $this->img_url);
            return "<img style='width:100%;' src='" . $big_img . "'>";
        });
        $grid->price('商品价格')->sortable();
        $grid->id('总销量')->display(function () {
            return GoodService::goodSellNumber($this->item_id, $this->title);
        });
        $grid->pinxiang('品相');
        $grid->rate('评分')->sortable();
        $grid->cloumn('多抓鱼搜索')->display(function () {
            $url = "https://www.duozhuayu.com/search/" . $this->title;
            return "<a target='_blank' href='" . $url . "'>" . $url . "</a>";
        });
        $states = [
            'on' => ['value' => 1, 'text' => '是', 'color' => 'primary'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'default'],
        ];
        $grid->column('tips', '到货提醒')->switch();
        $grid->updated_at('Updated at');

        $grid->filter(function ($filter) {

            $filter->disableIdFilter();

            $filter->like('title', '商品名');

            $filter->between('price', '价格');

        });
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Goods);

        $form->text('title', '标题');
        $form->radio('tips', '到货提醒')->options(['1' => '是', '0' => '否']);
        $states = [
            'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];

        $form->switch('tips', '到货提醒')->states($states);

        return $form;
    }
}
